package org.zjt.thread.interrupt;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.concurrent.FutureCallback;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager;
import org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor;
import org.apache.http.nio.reactor.IOReactorException;
import org.apache.http.util.EntityUtils;
import org.zjt.jsr.aa.Getter;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.LocalDateTime;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;

/**
 * Description:
 *
 * @author juntao.zhang
 * Date: 2018-05-31 下午2:33
 * @see
 */
@Getter
@Slf4j
public class NIOInterruptableOperation implements InterruptableOperation{

    private static  CloseableHttpAsyncClient client = HttpAsyncClientBuilder.create().build();

    static {
        client.start();
    }

    private static RequestConfigLoader requestConfigLoader = new RequestConfigLoader();


    private final LocalDateTime start ;

    private final URL url;

    private final HttpRequest httpRequest;

    public NIOInterruptableOperation(LocalDateTime start, URL url, HttpRequest httpRequest) {
        this.start = start;
        this.url = url;
        this.httpRequest = httpRequest;
    }

    public String doCall() throws  IOException {

        try {
            HttpGet httpGet = new HttpGet(url.toString());
            CountDownLatch countDownLatch = new CountDownLatch(1);
            client.execute(httpGet, new FutureCallback<HttpResponse>() {
                @Override
                public void completed(HttpResponse httpResponse) {
                    countDownLatch.countDown();
                    try {
                        log.info( "    111   "+EntityUtils.toString(httpResponse.getEntity()));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void failed(Exception e) {
                    countDownLatch.countDown();

                }

                @Override
                public void cancelled() {
                    countDownLatch.countDown();
                }
            });
            countDownLatch.await();
//            return EntityUtils.toString(closeableHttpResponse.getEntity());
        }catch (Exception e){
            throw new RuntimeException(e);
        }finally {
            //关闭stream流，但不是关闭链接，链接归还给pool
//            EntityUtils.consume(closeableHttpResponse.getEntity());
        }
        return "";
    }


    @Override
    public String call() throws Exception {
        return doCall();
    }

    @Override
    public LocalDateTime getStart() {
        return start;
    }


    public static void main(String[] args) throws Exception {
        URL url = new URL("https://www.baidu.com");
        NIOInterruptableOperation nioInterruptableOperation = new NIOInterruptableOperation(null,url,null);
        nioInterruptableOperation.call();
    }
}
